﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunergyLogic;
using System.Web.Security;

public partial class meter_AddNewManual : System.Web.UI.Page
{
    bool isUpdate;
    Guid meterGuid;
    MembershipUser thisUser = Membership.GetUser();
    communergyEntities db = new communergyEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        var meterGuidString = Request.QueryString["meterId"];

        isUpdate = Helpers.IsGuid(meterGuidString, out meterGuid);
       

        if (!Page.IsPostBack)
        {
            if (isUpdate)
            {
                btnAdd.Text = "Update";
                var getMeter = db.ElectricityMeter.Where(m => m.me_meterGuid == meterGuid).FirstOrDefault();
                txtMeterName.Text = getMeter.me_meterName;
                chkPrivate.Checked = getMeter.me_private.Value;
            }
            else 
            {
                txtMeterName.Text = string.Format("{0}'s electricity meter", thisUser.UserName);
            }
            panelEnter.Visible = true;
            panelSaved.Visible = false;

        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
        var meterName = txtMeterName.Text;
        
        Guid userGuid = new Guid(thisUser.ProviderUserKey.ToString());
        ElectricityMeter saveMeter = null;
        if (isUpdate)
        {
            saveMeter = db.ElectricityMeter.Where(m => m.me_meterGuid == meterGuid).FirstOrDefault();
            if (saveMeter != null)
            {
                saveMeter.me_meterName = meterName;
                saveMeter.me_private = chkPrivate.Checked;
            }
        }
        else
        {
            saveMeter = new ElectricityMeter { me_meterName = meterName, me_aspUserId = userGuid, me_private = chkPrivate.Checked, me_meterGuid = Guid.NewGuid() };
            db.AddToElectricityMeter(saveMeter);
        }
        var saved = db.SaveChanges();
        if (saved > 0)
        {
            panelEnter.Visible = false;
            panelSaved.Visible = true;

            var lblNewMeter = panelSaved.FindControl("lblNewMeter") as Label;
            if (lblNewMeter != null)
            {
                lblNewMeter.Text = saveMeter.me_meterName;
            }

            var hypAddReading = panelSaved.FindControl("hypAddReading") as HyperLink;
            if (hypAddReading != null)
            {
                hypAddReading.NavigateUrl = string.Format("~/manual/EnergyEntry_electricity.aspx?meterId={0}", saveMeter.me_id);
            }

            var hypUpdate = panelSaved.FindControl("hypUpdate") as HyperLink;
            if (hypUpdate != null)
            {
                hypUpdate.NavigateUrl = string.Format("?meterId={0}", saveMeter.me_meterGuid);
            }


        }

    }
    protected void chkPrivate_CheckedChanged(object sender, EventArgs e)
    {

    }
}
